import { axiosInstanceClient } from "../client_route";
import { axiosInstance } from "../route";
import { encrypt } from "@/utils/crypto";
import { ResponseType } from "@/utils/types";

async function APILogin(
  username: string,
  password: string,
  forcelogin: string
): Promise<ResponseType> {
  password = encrypt(password);
  return new Promise((resolve, reject) => {
    axiosInstanceClient()
      .post("/Author/Login/login", {
        username,
        password,
        forcelogin,
      })
      .then((res: ResponseType) => {
        resolve(res);
      })
      .catch((err: ResponseType) => {
        reject(err);
      });
  });
}

async function APICaptcha(): Promise<ResponseType> {
  const width = 250;
  const height = 40;
  return new Promise((resolve, reject) => {
    axiosInstance()
      .post("/Author/Login/captcha", { width, height },{withCredentials:true})
      .then((res: ResponseType) => {
        resolve(res);
      })
      .catch((err: ResponseType) => {
        reject(err);
      });
  });
}

export { APILogin, APICaptcha };
